<template>
	<el-container>
        <el-aside width="200px" v-loading="menuloading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="menuFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="menu" class="menu" node-key="id" :data="menuList" :props="menuProps" :current-node-key="''"
							 draggable highlight-current :expand-on-click-node="false"
							 :filter-node-method="menuFilterNode" @node-click="menuClick"></el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" @click="addType" v-auth="'craftCate.add'"></el-button>
					<el-button type="success" size="small" plain icon="el-icon-edit" @click="editMenu" v-auth="'craftCate.edit'"></el-button>
					<el-button type="danger" size="small" plain icon="el-icon-delete" @click="delMenu" v-auth="'craftCate.delete'"></el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
                <div class="left-panel">
					<el-button type="primary" @click="add" v-auth="'reason.add'">新增</el-button>
					<!-- <el-button type="danger" :disabled="selection.length == 0" @click="batch_del" v-auth="'reason.deleteAll'">删除</el-button> -->
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keywords" placeholder="搜索名称或编码" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="refresh">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable @row-dblclick="batch_edit" ref="table" :apiObj="apiObj" row-key="id"  @selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
					<el-table-column label="#" type="index" width="50" align="center"></el-table-column>
					<el-table-column label="编号" prop="code" align="center"></el-table-column>
					<el-table-column label="名称" prop="name" align="center"></el-table-column>
					<el-table-column label="检验标准" prop="standard" align="center"></el-table-column>
					<el-table-column label="方法" prop="method" align="center"></el-table-column>
					<el-table-column label="检验单位" prop="unit" align="center"></el-table-column>
					<el-table-column label="标准值" prop="stvalue" align="center"></el-table-column>
					<el-table-column label="检验上限" prop="upper" align="center"></el-table-column>
					<el-table-column label="检验下限" prop="lower" align="center"></el-table-column>
					<el-table-column label="备注" prop="note" align="center"></el-table-column>
                    <el-table-column label="操作" align="center" width="180">
                        <template #default="scope">
                            <el-button-group>
                                <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'reason.edit'">编辑</el-button>
                                <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                    <template #reference>
                                        <el-button text type="danger" size="small" v-auth="'reason.delete'">删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </el-button-group>
                        </template>
                    </el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <item-cate v-if="dialog.cate" ref="cateDialog" @success="cateHandleSuccess" @closed="dialog.cate = false"></item-cate>
</template>

<script>
import ItemCate from './itemCate.vue';
import saveDialog from "./save.vue";
export default {
	name: 'user',
	components: {
		saveDialog,
ItemCate
	},
	data() {
		return {
			dialog: {
				save: false,
				cate: false,
			},
			group: [],
			apiObj: this.$API.production.item.list,
			selection: [],
			search: {
				keywords: null,
			},
            menuloading: false,
			menuFilterText: "",
            menuList: [],
			menuProps: {
				label: (data) => {
					return data.group_name
				}
			},
			menuForm: {},
		}
	},
    watch: {
		menuFilterText(val) {
			this.$refs.menu.filter(val);
		}
	},
    mounted() {
		this.getMenu();
	},
	methods: {
		/**查看点击*/
		batch_show() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.production.item.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
				this.$refs.table.refresh();
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
		selectionChange(selection) {
			this.selection = selection;
		},
        //加载树数据
		async getMenu() {
			this.menuloading = true;
			var res = await this.$API.production.itemType.list.get();
			this.menuloading = false;
			var allNode = {id: '', group_name: '所有'}
			res.data.unshift(allNode);
			this.menuList = res.data;
			// this.menuClick(allNode)
		},
		//树点击
		menuClick(data) {
			this.menuForm = data
			var params = {
				group: data.id,
			}
			this.$refs.table.reload(params)
		},
		//树过滤
		menuFilterNode(value, data) {
			if (!value) return true;
			return data.group_name.indexOf(value) !== -1;
		},
		/**删除菜单*/
		async delMenu() {
			if (!this.menuForm.id) {
				this.$message.warning("请选择需要删除的项")
				return false;
			}
			var confirm = await this.$confirm('确认删除已选择的分类吗？', '提示', {
				type: 'warning',
				confirmButtonText: '删除',
				confirmButtonClass: 'el-button--danger'
			}).catch(() => {
			})
			if (confirm != 'confirm') {
				return false
			}
			this.menuloading = true
			var reqData = {
				id: this.menuForm.id
			}
			var res = await this.$API.production.itemType.delete.post(reqData)
			this.menuloading = false
			if (res.code == 1) {
				this.getMenu()
				this.menuForm = {}
			} else {
				this.$message.warning(res.message)
			}
		},
		/**添加分类*/
		addType() {
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('add')
			})
		},
		/**编辑分类*/
		editMenu() {
			if (!this.menuForm.id) {
				this.$message.warning("请选择需要编辑的项")
				return false;
			}
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('edit').setData(this.menuForm)
			})
		},
		/**分类添加成功事件*/
		cateHandleSuccess() {
			this.getMenu();
		},
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}

.shopInfo {
	display: flex;
	align-items: center;
}

.img {
	width: 100px;
	height: 100px;
	margin-right: 5px;
}
</style>
